package practice1_100;

public class Practice61 {
    public ListNode rotateRight(ListNode head, int k) {
        if(k==0||head==null||head.next==null) return head;
        int len=1;
        ListNode temp=head;
        while (temp.next!=null){
            len++;
            temp=temp.next;//此时temp就在链表的最后面了
        }
        k=k%len;
        if(k==0) return head;
        ListNode tempHead=head;
        for (int i = 0; i <len-k; i++) {
            tempHead=tempHead.next;
        }
        ListNode tempHead1=tempHead.next;
        tempHead.next=null;
        temp.next=head;
        return tempHead1;
    }

    public static void main(String[] args) {
        new Practice61().rotateRight(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4, new ListNode(5))))),2);
    }
}
